package com.qf;

import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.User;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class Demo1 {
    @Test
    public void set(){
        //连接Redis,默认连接localhost：6379
        Jedis jedis=new Jedis("192.168.163.128",6379);
        jedis.set("index","0");
        //String 赵英杰 = jedis.get("赵英杰");
        //System.out.println(赵英杰);
    }
    @Test
    public void testSaveByteArray(){
        User user =new User(1,"吴彦霞",22,new Date());
        Jedis jedis=new Jedis("192.168.163.128",6379);
        byte[] users = SerializationUtils.serialize(user);
        jedis.set("user".getBytes(), users);
        byte[] bytes = jedis.get("user".getBytes());
        User user1 =(User) SerializationUtils.deserialize(bytes);
        System.out.println(user1);
        jedis.close();
    }
    @Test
    public void testSaveJson(){
        Jedis jedis=new Jedis("192.168.163.128",6379);
//        User user=new User(2,"小吴",22,new Date());
//        //把user对象转换成JSON
//        String s = JSONObject.toJSONString(user);
//        jedis.set("user1",s);
        //取出数据库中的json转换成对象
        String user11 = jedis.get("user1");
        User user = JSONObject.parseObject(user11, User.class);
        System.out.println(user);
        jedis.close();
    }

}
